package com.example.hcbmsm.service.impl;

import com.auth0.jwt.interfaces.Claim;
import com.example.hcbmsm.domain.Reg;
import com.example.hcbmsm.domain.RegExample;
import com.example.hcbmsm.mapper.RegMapper;
import com.example.hcbmsm.service.RegService;
import com.example.hcbmsm.util.HCBMSMResult;
import com.example.hcbmsm.util.JavaWebToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Xinghm
 * @version 1.0
 * @since 2024/10/29 22:58
 */
@Service
public class RegServiceImpl implements RegService {
    @Autowired
    private RegMapper regMapper;

    @Override
    public Map add(String token, Reg reg) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        reg.setPatId(new Long(tokenInfo.get("patId").asString()));
        if (regMapper.insertSelective(reg) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map findAllReg(String token) {
        Map<String, Claim> tokenInfo = JavaWebToken.getTokenInfo(token);
        Long patId = new Long(tokenInfo.get("patId").asString());
        return HCBMSMResult.SUCCESS(regMapper.selectRegById(patId));
    }

    @Override
    public Map renewBYOpId(String token, Reg reg) {
        reg.setModifyTime(new Date());
        RegExample regExample = new RegExample();
        regExample.createCriteria().andOpIdEqualTo(reg.getOpId());
        if (regMapper.updateByExampleSelective(reg, regExample) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map remove(Long regId) {
        if (regMapper.deleteByPrimaryKey(regId) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }

    @Override
    public Map renew(String token, Reg reg) {
        reg.setModifyTime(new Date());
        if (regMapper.updateByPrimaryKeySelective(reg) == 1) {
            return HCBMSMResult.SUCCESS();
        }
        return HCBMSMResult.ERROR();
    }
}
